#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: migrate_clamp_database.py
# @time: 2021/3/30 19:20
# @desc:
import os

import pymongo

from .views.configs import *
from .utils.md5_tools import getFileMd5
from .utils.oss.oss import upload_file_to_oss
from .utils.utils import read_json

clamp_root_path = r"data/history_clamp"

oss_file_dirname = "DOCenter_Dev/Pythonfiles/new_data/data/history_clamp"

new_clamp_list = []

myclient = pymongo.MongoClient(MONGO_URL)
mydb = myclient["do_center"]
clamp = mydb["history_clamp"]

clamp_list = []
for i in clamp.find({}):
    i.pop("_id")
    i["file"][STP_OSS_ADDRESS] = i["file"][CATPART_OSS_ADDRESS].replace(".CATPart", ".stp")
    if "history_clamp" in i["file"][CATPART_OSS_ADDRESS]:
        filename = i["file"][CATPART_OSS_ADDRESS].split("history_clamp/")[1]
        stp_file = f"{clamp_root_path}/{filename}".replace(".CATPart", ".stp")
        i["file"]["stpMD5"] = getFileMd5(stp_file)
    print(i)
    clamp_list.append(i)

clamp.insert_many(clamp_list)